package org.aplikator.client.local.command;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import org.aplikator.client.local.Aplikator;
import org.aplikator.client.local.wizards.WizardSupport;
import org.aplikator.client.local.wizards.WizardSupportCallbacks;
import org.aplikator.client.shared.descriptor.FunctionDTO;
import org.aplikator.client.shared.descriptor.WizardPageDTO;
import org.jboss.errai.common.client.api.RemoteCallback;

public class ExecuteFunction extends AplikatorActivity implements WizardSupportCallbacks {


    public ExecuteFunction(String functionId) {
        super(functionId);
    }

    private FunctionDTO function;
    private WizardSupport wizardSupport;

    @Override
    public void start(final AcceptsOneWidget panel, EventBus eventBus) {
        
        if (function == null) {
            Aplikator.aplikatorService.call( new RemoteCallback<FunctionDTO>() {
                @Override
                public void callback(FunctionDTO response) {
                    ExecuteFunction.this.function = response;
                    drawPanel(panel);
                }
            }).getFunction(getToken());
        } else {
            drawPanel(panel);
        }

    }

    private void drawPanel(AcceptsOneWidget panel) {
        this.wizardSupport = new WizardSupport(function, null, this);
        this.wizardSupport.changePageRequest(true);
        
        
        /**
        panel.setWidget(new Label("Started function " + function.getLocalizedName()));
        Record functionParameters = new Record();// TODO functionParameters from
                                         // function descriptor and wizard

        Aplikator.aplikatorService.call( new RemoteCallback<String>() {
            @Override
            public void callback(String response) {
                Window.alert(response); // TODO better response
            }
        }).runFunction(new ParameterWrapperRunFunction(function, Aplikator.clientContext.clone(), functionParameters));
        */
    }

    @Override
    public void wizardPageCallback(WizardPageDTO response, boolean forw) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void runBeforeFunctionCallback() {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void runAfterFunctionCallback() {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    
}
